package sketch.specs;

import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.internal.core.dom.NaiveASTFlattener;

import sketch.ast.ASTUtils;
import sketch.util.Checker;
import sketch.util.Globals;

public class AppendExpandedMethodVisitor extends NaiveASTFlattener {
	
	public static boolean remove_original_method = true;
	
	public final CompilationUnit unit;
	public final Map<MethodDeclaration, List<MethodDeclaration>> maps;
	public AppendExpandedMethodVisitor(CompilationUnit unit, Map<MethodDeclaration, List<MethodDeclaration>> maps) {
		Checker.checkNull(unit, "The unit could not be null.");
		Checker.checkNull(maps, "the method maps could not be null.");
		this.unit = unit;
		this.maps = maps;
	}
	@Override
	public boolean visit(MethodDeclaration method) {
		boolean ret = false;
		
		MethodDeclaration matched_method = ASTUtils.getMethodBySignature(maps.keySet(), method);
		if(matched_method != null) {
			List<MethodDeclaration> replicates = maps.get(matched_method);
			if(!remove_original_method) {
				super.visit(method);
			}
			//append to the unit
			for(MethodDeclaration replicate : replicates ) {
				super.buffer.append(Globals.lineSep);
				super.buffer.append(replicate.toString());
				super.buffer.append(Globals.lineSep);
			}
		} else {
			ret = super.visit(method);
		}
		
		return ret;
	}
}
